package com.probox.modules.controller.platfom;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.probox.common.core.constant.HttpStatus;
import com.probox.common.core.enums.BuyTypeEnum;
import com.probox.common.core.enums.SourcePlatformEnum;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.utils.money.MoneyUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.customer.CustomerInfo;
import com.probox.common.entity.domain.order.OrderInfoItem;
import com.probox.common.entity.service.platfom.impl.StatisticsService;
import com.probox.common.entity.vo.customerService.CustomerServiceBuyRecordVo;
import com.probox.common.entity.vo.recharge.RechargeVo;
import com.probox.common.entity.vo.statistics.*;
import com.probox.customer.api.domain.CustomerCollect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private StatisticsService statisticsService;

    @GetMapping("/selectProductSale")
    public AjaxResult selectProductSale(ProductSaleVo vo) {
        return statisticsService.selectProductSale(vo);
    }

    @GetMapping("/selectItemByProUidAndType")
    public TableDataInfo selectItemByProUidAndType(ProductSaleVo vo) {
        startPage();
        List<OrderInfoItem> orderInfoItems = statisticsService.selectItemByProUidAndType(vo);
        return getDataTable(orderInfoItems);
    }


    @PostMapping("/selectProductSaleExport")
    public void export(HttpServletResponse response, ProductSaleVo vo) throws IOException {
        List<ProductSaleVo> list = statisticsService.selectProductSaleExport(vo);
        ExcelUtil<ProductSaleVo> util = new ExcelUtil<ProductSaleVo>(ProductSaleVo.class);
        list.forEach(vos -> {
            vos.setAmountProportionStr(vos.getAmountProportionStr());
            vos.setNumberProportionStr(vos.getNumberProportionStr());
            vos.setAmountStr(vos.getAmount()/100.00);
        });
        util.exportExcel(response, list, "门店销售数据");
    }


    @GetMapping("/selectCardConsumptionSale")
    public AjaxResult selectCardConsumptionSale(CardConsumptionSaleVo vo) {
        if (StringUtils.isEmpty(vo.getStartTime()) || StringUtils.isEmpty(vo.getEndTime())){
            AjaxResult ajaxResult = new AjaxResult();
            List<CardConsumptionSaleVo> productSaleVos = new ArrayList<>();
            ajaxResult.put("rows", productSaleVos);
            ajaxResult.put("code", HttpStatus.SUCCESS);
            ajaxResult.put("msg", "查询成功");
            ajaxResult.put("total", new PageInfo(productSaleVos).getTotal());
            CardConsumptionSaleVo data = new CardConsumptionSaleVo();
            data.setAmount(0L);
            data.setNumber(0L);
            ajaxResult.put("data",data);
            return ajaxResult;
        }
        if (isAdmin() || (vo.getStoreIds() != null && vo.getStoreIds().size()>0 )){
            return statisticsService.selectCardConsumptionSale(vo);
        }else {
            AjaxResult ajaxResult = new AjaxResult();
            List<CardConsumptionSaleVo> productSaleVos = new ArrayList<>();
            ajaxResult.put("rows", productSaleVos);
            ajaxResult.put("code", HttpStatus.SUCCESS);
            ajaxResult.put("msg", "查询成功");
            ajaxResult.put("total", new PageInfo(productSaleVos).getTotal());
            CardConsumptionSaleVo data = new CardConsumptionSaleVo();
            data.setAmount(0L);
            data.setNumber(0L);
            ajaxResult.put("data",data);
            return ajaxResult;
        }
    }

    /**
     * 导出耗卡明细
     * @param response
     * @param vo
     * @throws IOException
     */
    @PostMapping("/selectCardConsumptionSaleDetailExport")
    public void exportDetail(HttpServletResponse response, CardConsumptionSaleVo vo) throws IOException {
        List<SubscribeDetailVo> list = new ArrayList<>();
        if (isAdmin() || (vo.getStoreIds() != null && vo.getStoreIds().size()>0 )){
            list = statisticsService.selectCardConsumptionSaleDetail(vo);
        }
        ExcelUtil<SubscribeDetailVo> util = new ExcelUtil<SubscribeDetailVo>(SubscribeDetailVo.class);
        list.forEach(vos -> {
            String name = SourcePlatformEnum.getNameByKey(vos.getSource());
            if (StringUtils.isNotEmpty(name)) {
                vos.setSource(name);
            }
            name = BuyTypeEnum.getNameByKey(vos.getBuyType());
            if (StringUtils.isNotEmpty(name)) {
                vos.setBuyType(name);
            }else{
                vos.setBuyType("三方");
            }
        });
        util.exportExcel(response, list, "卡耗订单表");
    }


    @PostMapping("/selectCardConsumptionSaleExport")
    public void export(HttpServletResponse response, CardConsumptionSaleVo vo) throws IOException {
        List<CardConsumptionSaleVo> list = new ArrayList<>();
        if (isAdmin() || (vo.getStoreIds() != null && vo.getStoreIds().size()>0 )){
            list = statisticsService.selectCardConsumptionSaleExport(vo);
        }
        ExcelUtil<CardConsumptionSaleVo> util = new ExcelUtil<CardConsumptionSaleVo>(CardConsumptionSaleVo.class);
        list.forEach(vos -> {
            vos.setAmountProportionStr(vos.getAmountProportionStr());
            vos.setNumberProportionStr(vos.getNumberProportionStr());
            vos.setHaoKa(new BigDecimal(vos.getAmount()/100.0).setScale(2, RoundingMode.HALF_UP).doubleValue());
        });
        util.exportExcel(response, list, "门店卡耗数据");
    }


    @GetMapping("/selectCardConsumptionSaleByProUidAndType")
    public TableDataInfo selectCardConsumptionSaleByProUidAndType(CardConsumptionSaleDetailVo vo) {
        startPage();
        List<CardConsumptionSaleDetailVo> cardConsumptionSaleDetailVos = statisticsService.selectCardConsumptionSaleByProUidAndType(vo);
        return getDataTable(cardConsumptionSaleDetailVos);
    }

    @GetMapping("/selectStoreManagementVo")
    public TableDataInfo selectStoreManagementVo(StoreManagementVo vo) {
        List<StoreManagementVo> cardConsumptionSaleDetailVos = new ArrayList<>();
        if (!StringUtils.isEmpty(vo.getStartTime()) && !StringUtils.isEmpty(vo.getEndTime())){
            if (vo.getStoreId() != null || isAdmin()) {
                startPage();
                cardConsumptionSaleDetailVos = statisticsService.selectStoreManagementVo(vo);
            }
        }
        return getDataTable(cardConsumptionSaleDetailVos);
    }

    @GetMapping("/getStoreManagementDetail")
    public TableDataInfo getStoreManagementDetail(StoreManagementVo vo) {
        startPage();
        List<?> storeManagementDetail = statisticsService.getStoreManagementDetail(vo);
        return getDataTable(storeManagementDetail);
    }


    @GetMapping("/selectStoreRechargeVo")
    public TableDataInfo selectStoreRechargeVo(StoreRechargeVo vo) {

        startPage();
        List<StoreRechargeVo> cardConsumptionSaleDetailVos = null;
        if (vo.getStoreId() == null && !isAdmin()){
            cardConsumptionSaleDetailVos = new ArrayList<>();
        }else{
            if (StringUtils.isNotEmpty(vo.getStartTime()) && StringUtils.isNotEmpty(vo.getEndTime())) {
                cardConsumptionSaleDetailVos = statisticsService.selectStoreRechargeVo(vo);
            }else{
                cardConsumptionSaleDetailVos = new ArrayList<>();
            }
        }
        return getDataTable(cardConsumptionSaleDetailVos);
    }


    @GetMapping("/selectBeauticianRankingVo")
    public AjaxResult selectBeauticianRankingVo(BeauticianRankingVo vo) {
        return statisticsService.selectBeauticianRankingVo(vo);
    }


    @GetMapping("/selectBeauticianRankingDetail")
    public TableDataInfo selectCardConsumptionSaleByProUidAndType(BeauticianRankingVo vo) {
        List<CardConsumptionSaleDetailVo> cardConsumptionSaleDetailVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(vo.getStartTime()) && StringUtils.isNotEmpty(vo.getEndTime())){
            startPage();
            cardConsumptionSaleDetailVos = statisticsService.selectBeauticianRankingDetail(vo);
        }
        return getDataTable(cardConsumptionSaleDetailVos);
    }

    @GetMapping("/selectRegistrationConversion")
    public AjaxResult selectRegistrationConversion(RegistrationConversionVo vo) {
        return AjaxResult.success(statisticsService.selectRegistrationConversion(vo));
    }

    /**
     * 根据时间查询注册人数
     * @param time
     * @return
     */
    @GetMapping("/getCustomerInfoDetail")
    public TableDataInfo getCustomerInfoDetail(String time){
        startPage();
        List<CustomerInfo> customerInfoDetail = statisticsService.getCustomerInfoDetail(time);
        return getDataTable(customerInfoDetail);
    }
    @GetMapping("/getStaticOrder")
    public TableDataInfo getStaticOrder(StoreManagementVo vo){
        startPage();
        List<OrderInfoItem> staticOrder = statisticsService.getStaticOrder(vo);
        return getDataTable(staticOrder);
    }

    @GetMapping("/getCustomerInfoDetailType")
    public TableDataInfo   getCustomerInfoDetailType(String time,String type,Long storeId){
        startPage();
        List<CustomerInfo> customerInfoDetail = statisticsService.getCustomerInfoDetailType(time,type,storeId);
        return getDataTable(customerInfoDetail);
    }

    @GetMapping("/getCustomerInfoOrderDetail")
    public TableDataInfo   getCustomerInfoOrderDetail(String time){
        startPage();
        List<CustomerInfo> customerInfoDetail = statisticsService.getCustomerInfoOrderDetail(time);
        return getDataTable(customerInfoDetail);
    }

    @GetMapping("/selectChannelVo")
    public AjaxResult selectChannelVo(BeauticianRankingVo vo) {
        if (vo.getStoreId() != null || isAdmin()) {
            return statisticsService.selectChannelVo(vo);
        }else{
            AjaxResult ajaxResult = new AjaxResult();
            List<ChannelVo> channelVos = new ArrayList<>();
            ajaxResult.put("rows", channelVos);
            ajaxResult.put("code", HttpStatus.SUCCESS);
            ajaxResult.put("msg", "查询成功");
            ajaxResult.put("total", new PageInfo(channelVos).getTotal());
            ajaxResult.put("data", new ChannelVo());
            return ajaxResult;
        }
    }

    @GetMapping("/selectOtherVo")
    public AjaxResult selectOtherVo(OtherVo vo) {
        if (vo.getStoreId() != null || isAdmin()) {
            return statisticsService.selectOtherVo(vo);
        }
        AjaxResult ajaxResult = new AjaxResult();
        List<OtherVo> otherVos = new ArrayList<>();
        HashMap map = new HashMap();
        map.put("data", otherVos);
        ajaxResult.put("rows", map);
        ajaxResult.put("code", HttpStatus.SUCCESS);
        ajaxResult.put("msg", "查询成功");
        ajaxResult.put("data", new OtherVo());
        return ajaxResult;
    }


    @GetMapping("/selectOtherVoTimes")
    public AjaxResult selectOtherVoTimes(OtherVo vo) {
        if (vo.getStoreId() != null || isAdmin()) {
            return statisticsService.selectOtherVoTimes(vo);
        }
        AjaxResult ajaxResult = new AjaxResult();
        List<OtherVo> otherVos = new ArrayList<>();
        HashMap map = new HashMap();
        map.put("data", otherVos);
        ajaxResult.put("rows", map);
        ajaxResult.put("code", HttpStatus.SUCCESS);
        ajaxResult.put("msg", "查询成功");
        ajaxResult.put("data", new OtherVo());
        return ajaxResult;
    }

    @GetMapping("/selectReconciliationVo")
    public AjaxResult selectReconciliationVo(ReconciliationVo vo) {

        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(vo.getStartTime()) || StringUtils.isEmpty(vo.getEndTime())){
            List<ReconciliationVo> reconciliationVos = new ArrayList<>();

            //HashMap map = new HashMap();
            ajaxResult.put("rows", reconciliationVos);
            ajaxResult.put("manCount", 0L);
            ajaxResult.put("itemCount", 0L);
            ajaxResult.put("kuaDianShouRu", 0L);

            ajaxResult.put("kuaDianZhiChu", 0L);
            ajaxResult.put("code", HttpStatus.SUCCESS);
            ajaxResult.put("msg", "查询成功");
            ajaxResult.put("total",0L);
            return ajaxResult;
        }
        if (vo.getStoreId() != null || isAdmin()) {
            startPage();
            List<ReconciliationVo> reconciliationVos = statisticsService.selectReconciliationVo(vo);
            //return getDataTable(reconciliationVos);

            //HashMap map = new HashMap();
            ajaxResult.put("rows", reconciliationVos);
            ajaxResult.put("manCount", statisticsService.selectReconciliationVoManCount(vo).getManCount());
            ajaxResult.put("itemCount", statisticsService.selectReconciliationVoItemCount(vo).getServiceAmount());
            vo.setCountType("1");
            ajaxResult.put("kuaDianShouRu", statisticsService.selectReconciliationVoKuaDianShouRuAndZhiChu(vo).getKuaDian());

            //Long payStoreId = vo.getPayStoreId();
            //vo.setPayStoreId(vo.getStoreId());
            //vo.setStoreId(payStoreId);
            vo.setCountType("2");
            ajaxResult.put("kuaDianZhiChu", statisticsService.selectReconciliationVoKuaDianShouRuAndZhiChu(vo).getKuaDian());
            ajaxResult.put("code", HttpStatus.SUCCESS);
            ajaxResult.put("msg", "查询成功");
            ajaxResult.put("total", new PageInfo(reconciliationVos).getTotal());
        }else{

            List<ReconciliationVo> reconciliationVos = new ArrayList<>();

            //HashMap map = new HashMap();
            ajaxResult.put("rows", reconciliationVos);
            ajaxResult.put("manCount", 0L);
            ajaxResult.put("itemCount", 0L);
            ajaxResult.put("kuaDianShouRu", 0L);

            ajaxResult.put("kuaDianZhiChu", 0L);
            ajaxResult.put("code", HttpStatus.SUCCESS);
            ajaxResult.put("msg", "查询成功");
            ajaxResult.put("total",0L);
        }
        return ajaxResult;
    }

    @GetMapping("/selectNotUsedBuyRecord")
    public TableDataInfo selectNotUsedBuyRecord(BuyRecordNotUsed name) {
        List<BuyRecordNotUsed> buyRecordNotUseds = null;
        if (name.getStoreId() != null || isAdmin()) {
            startPage();
            buyRecordNotUseds = statisticsService.selectNotUsedBuyRecord(name);
        }else{
            buyRecordNotUseds = new ArrayList<>();
        }
        return getDataTable(buyRecordNotUseds);
    }

    /**
     * 导出卡包
     * @param response
     * @param name
     * @throws IOException
     */
    @PostMapping("/exportSelectNotUsedBuyRecord")
    public void exportSelectNotUsedBuyRecord(HttpServletResponse response,BuyRecordNotUsed name) throws IOException  {
        List<BuyRecordNotUsed> buyRecordNotUseds = null;
        if (name.getStoreId() != null || isAdmin()) {
            buyRecordNotUseds = statisticsService.selectNotUsedBuyRecord(name);
        }else{
            buyRecordNotUseds = new ArrayList<>();
        }
        ExcelUtil<BuyRecordNotUsed> util = new ExcelUtil<BuyRecordNotUsed>(BuyRecordNotUsed.class);
        util.exportExcel(response, buyRecordNotUseds, "卡包使用统计");
    }

    @GetMapping("/getNotUsedBuyRecordDetail")
    public TableDataInfo getNotUsedBuyRecordDetail(BuyRecordNotUsed used) {
        startPage();
        if (StringUtils.isEmpty(used.getUsed())){
            used.setUsed("100");
        }
        List<CustomerServiceBuyRecordVo> notUsedBuyRecordDetail = statisticsService.getNotUsedBuyRecordDetail(used);
        return getDataTable(notUsedBuyRecordDetail);
    }

}
